package com.ex.orage2app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPicker;



// 注册
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{
private TextView cityText;
private CityPicker citypicker;
private EditText phone_edit,paswd_edit,surepasswd;
private RadioGroup sex_group;
private CheckBox read,play,music;
private Button register;
//定义字符串保存信息
    private String phone_str="";
    private String paswd_str="";
    private String hobby_str="1";
    private String city="";
    private String sex_str="";
    private String surepaswd_str="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }
    //组件初始化
    private void init(){
        phone_edit=findViewById(R.id.phone);
        paswd_edit=findViewById(R.id.paswd);
        sex_group=findViewById(R.id.sex);
        surepasswd=findViewById(R.id.surepaswd);
        sex_group.setOnCheckedChangeListener(this);

        read=findViewById(R.id.read_book);
        play=findViewById(R.id.play_ball);
        music=findViewById(R.id.music);
        register=findViewById(R.id.reg_register);
        register.setOnClickListener(this);


        cityText=findViewById(R.id.reg_provice);
        cityText.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.reg_provice) {
            initCityPicker();
            citypicker.show();
        } else if (id == R.id.reg_register) {//获取手机号和密码
            phone_str = phone_edit.getText().toString();
            paswd_str = paswd_edit.getText().toString();
            surepaswd_str = surepasswd.getText().toString();
            hobby_str = "";
            if (read.isChecked()) {
                hobby_str += read.getText().toString();
            }
            if (play.isChecked()) {
                hobby_str += play.getText().toString();
            }
            if (music.isChecked()) {
                hobby_str += music.getText().toString();
            }
            if (paswd_str.equals(surepaswd_str)) {
                if (!phone_str.equals("") || !paswd_str.equals("")) {
                    if (!city.equals("")) {
                        //跳转传值

                        Bundle bundle = new Bundle();
                        bundle.putString("username", phone_str);
                        bundle.putString("password", paswd_str);
                        bundle.putString("sex", sex_str);
                        bundle.putString("hobby", hobby_str);
                        bundle.putString("city", city);
                        //使用安卓自带的SQLite数据库储存注册的信息
                        OrangeDatabase orangeDatabase = new
                                OrangeDatabase(RegisterActivity.this);
                        SQLiteDatabase sqLiteDatabase = orangeDatabase.getWritableDatabase();
                        insertData(sqLiteDatabase, bundle);

                        Intent intent = new Intent(this, IndexActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);


                    } else {
                        Toast.makeText
                                (RegisterActivity.this, "请选择地址", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText
                            (RegisterActivity.this, "账号或密码未填写", Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText
                        (RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_LONG).show();
            }
        }

    }
    public void initCityPicker(){

   citypicker=new CityPicker.Builder(RegisterActivity.this)
           .textSize(16)
           .province("xx省")
           .city("xx市")
           .district("xx区")
           .build();
   citypicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
       @Override
       public void onSelected(String... strings) {
           String p=strings[0];
           String c=strings[1];
           String d=strings[2];
           cityText.setText(String.format("%s %s %s",p,c,d));
           city=p+c+d;

       }

       @Override
       public void onCancel() {

       }
   });




    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        sex_str= i == R.id.nan? "男性":"女性";

    }
    private void insertData(SQLiteDatabase sqLiteDatabase,Bundle bundle){
        ContentValues contentValues=new ContentValues();
        contentValues.put("username", bundle.getString("username"));
        contentValues.put("password", bundle.getString("password"));
        contentValues.put("sex", bundle.getString("sex"));
        contentValues.put("city", bundle.getString("city"));
        sqLiteDatabase.insert("orange_user",null,contentValues);
        sqLiteDatabase.close();
    }
}